package com.webtest.tokentest;

import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/8/8 14:20
 */
public class TokenServlet extends HttpServlet {
    private static final long serialVersionUID = 4848820354695727889L;
    private static final Logger LOGGER = LoggerFactory.getLogger(TokenServlet.class);

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*String token = req.getParameter("token");
        if ("tk".equals(token)) {
            //清除标记：没有方法清除固定的请求参数
        }*/
        String name = req.getParameter("name");
        HttpSession session = req.getSession();

        boolean valid = TokenProcessor.getInstance().isTokenValid(req);
        if (valid) {
            TokenProcessor.getInstance().resetToken(req);
        } else {
            resp.sendRedirect(req.getContextPath()+"/token/token.jsp");
            return;
        }

        /*String token = (String) session.getAttribute("token");
        String tokenValue = req.getParameter("token");
        LOGGER.info("token:" + token);
        LOGGER.info("tokenValue:" + tokenValue);

        if (null != token && token.equals(tokenValue)) {
            // 移除请求
            session.removeAttribute("token");
            LOGGER.info("remove token");
        } else {
            resp.sendRedirect(req.getContextPath()+"/token/token.jsp");
            return;
        }*/

        LOGGER.info("name:" + name);
        TimeUnit.SECONDS.sleep(30);
//        req.getRequestDispatcher("/token/success.jsp").forward(req, resp);
        resp.sendRedirect(req.getContextPath()+"/token/success.jsp");
    }
}
